package com.zmcn.msg.business.msgOrder.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zmcn.msg.bean.po.MsgOrder;
import com.zmcn.msg.bean.po.OrderGroup;
import com.zmcn.msg.bean.vo.UserVO;
import com.zmcn.msg.business.msgOrder.dao.MsgOrderDao;
import com.zmcn.msg.business.msgOrder.service.MsgOrderService;
import com.zmcn.msg.business.msgOrder.vo.MsgOrderVO;
import com.zmcn.msg.fw.action.Page;

/**
 * 
 * 消息订单业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class MsgOrderServiceImpl implements MsgOrderService {
	private MsgOrderDao dao;

	@Autowired
	public void setDao(MsgOrderDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取消息订单列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public Page<MsgOrderVO> getList(Page<MsgOrderVO> page) {
		Page<MsgOrderVO> voPage =  dao.getList(page);
		List<MsgOrderVO> voList = voPage.getResult();
		List<String> orderIds = new ArrayList<String>();
		for(MsgOrderVO vo : voList){
			orderIds.add(vo.getOrderId());
		}
		if(CollectionUtils.isNotEmpty(orderIds)){//查询接收群并组装
			Map<String, String> groupName = dao.getReceivers(orderIds);
			if(!groupName.isEmpty()){
				for(MsgOrderVO vo : voList){
					vo.setGroupName(groupName.get(vo.getOrderId()));
				}
			}
		}
		return voPage;
	}

	/**
	 * <p>Title: sendmsg</p>
	 * <p>Description: TODO 下发消息订单</p>
	 * @param orderIds
	 * @param user
	 * @return
	 * int
	 * @throws
	 * @author 谭永戈
	 * 2012-12-7 下午5:06:55
	 */
	@Transactional(readOnly = false)
	public int sendmsg(String[] orderIds, UserVO user){
		return dao.sendmsg(orderIds, user);
	}
	
	/**
	 * 根据ID获取消息订单对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	public MsgOrderVO getMsgOrderVO(String orderId) {
		return dao.getMsgOrderVO(orderId);
	}

	/**
	 * 增加消息订单
	 * 
	 * @param msgId
	 *            消息ID
	 * @param groups
	 *            用户群
	 * @param user
	 *            当前用户
	 */
	@Transactional(readOnly = false)
	public void add(String msgId, String[] groups, String user) {
		Date date = new Date();
		MsgOrder msgOrder = new MsgOrder();
		msgOrder.setMsgId(msgId);
		msgOrder.setCreator(user);
		msgOrder.setCreateTime(date);
		dao.create(msgOrder);
		for (String group : groups) {
			OrderGroup og = new OrderGroup();
			og.setOrderId(msgOrder.getOrderId());
			og.setGroupId(group);
			dao.create(og);
		}
	}

	/**
	 * 修改消息订单
	 * 
	 * @param orderId
	 *            订单ID
	 * @param msgId
	 *            消息ID
	 * @param creator
	 *            创建者
	 * @param createTime
	 *            创建时间
	 * @param sendor
	 *            下发者
	 * @param sendTime
	 *            下发时间
	 * @param user
	 *            当前用户
	 */
	@Transactional(readOnly = false)
	public void edit(String orderId, String msgId, String creator, Date createTime, String sendor, Date sendTime, String user) {
		MsgOrder msgOrder = (MsgOrder) dao.queryById(MsgOrder.class, orderId);
		msgOrder.setOrderId(orderId);
		msgOrder.setMsgId(msgId);
		msgOrder.setCreator(creator);
		msgOrder.setCreateTime(createTime);
		msgOrder.setSendor(sendor);
		msgOrder.setSendTime(sendTime);
		dao.update(msgOrder);
	}

	/**
	 * 删除消息订单
	 * 
	 * @param ids
	 *            多个ID
	 * @param user
	 *            当前用户
	 * @return
	 */
	@Transactional(readOnly = false)
	public void remove(String[] ids, String user) {
		dao.remove(ids, user);
	}

}
